﻿package com.danielsheldon.utils.displayListMonitor
{
	import flash.display.Sprite;
	import flash.display.MovieClip;
	import flash.text.TextField;
	import flash.net.LocalConnection;
	import flash.events.MouseEvent;
	import fl.controls.UIScrollBar;

	public class DLMReciever extends Sprite
	{
		private var _conn:LocalConnection;
		private var _msgArray:Array = new Array();
		private var _filterObj:Object = new Object();
		private var _msgXML:XML;
		private var _curMsg:Number = 0;
		private var _totMsg:Number = 0;
		private var _displayXML:XMLList = new XMLList();
		private var _changed:Boolean = false;

		public function DLMReciever()
		{
			_filterObj = {added:true, addedToStage:true, removed:true, removedFromStage:true};
			_conn = new LocalConnection();
			_conn.client = this;
			_msgXML = <messages></messages>;
			trace(_msgXML);
			try
			{
				_conn.connect("DLMConnection");
			}
			catch (error:ArgumentError)
			{
				trace("Can't connect...the connection name is already being used by another SWF");

			}
			next.buttonMode = true;
			prev.buttonMode = true;
			added.buttonMode = true;
			addedToStage.buttonMode = true;
			removed.buttonMode = true;
			removedFromStage.buttonMode = true;
			added.addEventListener(MouseEvent.CLICK, updateFilter);
			addedToStage.addEventListener(MouseEvent.CLICK, updateFilter);
			removed.addEventListener(MouseEvent.CLICK, updateFilter);
			removedFromStage.addEventListener(MouseEvent.CLICK, updateFilter);
			next.addEventListener(MouseEvent.CLICK, changeMsg);
			prev.addEventListener(MouseEvent.CLICK, changeMsg);
		}
		private function changeMsg(e:MouseEvent)
		{
			if (e.target.name == "next")
			{
				_curMsg++;
				if (_curMsg >= _displayXML.length())
				{
					_curMsg = 0;
				}
			} else
			{
				_curMsg--;
				if (_curMsg < 0)
				{
					_curMsg = _displayXML.length() -1;
				}
			}
			displayMessage();
		}
		public function DLMHandler(type:String, msg:String, time:int, msgNum:int):void
		{
			// xml:
			//trace("<message type='"+type+"'>"+msg+"</message>");
			//_msgXML.appendChild(XML("<message type='"+type+"'>"+msg+"</message>"));
			_msgXML.appendChild(XML("<"+type+" time='"+time+"' msgNum='"+msgNum+"'>"+msg+"</"+type+">"));
			// array
			_msgArray.push(msg);
			_totMsg = _msgArray.length;
			_curMsg = _totMsg - 1;
			_changed = true;
			displayMessage();
		}
		private function displayMessage()
		{
			getOutput();
			page.text = (_curMsg+1) + "/" + _displayXML.length();
			output.text = _displayXML[_curMsg];
			scrollBar.update();
		}
		private function updateFilter(e:MouseEvent)
		{
			_changed = true;
			_filterObj[e.target.name] = !_filterObj[e.target.name];
			if(_filterObj[e.target.name])
			{
				e.target.gotoAndStop(1);
			}else{
				e.target.gotoAndStop(2);
			}
			displayMessage();
		}
		private function getOutput()
		{
			if(_changed)
			{
				_displayXML = new XMLList();
				_displayXML = _msgXML.DLM;
				if (_filterObj.added)
				{
					_displayXML+=_msgXML.added;
				}
				if (_filterObj.addedToStage)
				{
					_displayXML+=_msgXML.addedToStage;
				}
				if (_filterObj.removed)
				{
					_displayXML+=_msgXML.removed;
				}
				if (_filterObj.removedFromStage)
				{
					_displayXML+=_msgXML.removedFromStage;
				}
				sortMsgs();
				_curMsg = _displayXML.length()-1;
				_changed = false;
			}
		}
		private function sortMsgs()
		{
			var _tmpList:XML = <messages></messages>;
			_tmpList.appendChild(_displayXML[0].toXMLString());
			test:for (var i:int = 1; i<_displayXML.length(); i++)
			{
				for (var j:int = 0; j<_tmpList.children().length(); j++)
				{
					//trace("dl", _displayXML[i].@msgNum, "tl", XML(_tmpList.children()[j].toXMLString()).@msgNum)
					if (Number(_displayXML[i].@msgNum) < Number(XML(_tmpList.children()[j].toXMLString()).@msgNum))
					{
					_tmpList.insertChildBefore(_tmpList.children()[j], _displayXML[i]);
					continue test;
					}
				}
				_tmpList.appendChild(_displayXML[i].toXMLString());
			}
			_displayXML = _tmpList.children();
		}
	}
}